package com.cmdi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cmdi.constant.CommonConstants;
import com.cmdi.domain.R;
import com.cmdi.domain.SysRouteConf;
import com.cmdi.exception.CustomCmdiAdminException;
import com.cmdi.feign.RemoteGatewayServiceFeignClient;
import com.cmdi.service.SysRouteConfService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.FilterDefinition;
import org.springframework.cloud.gateway.handler.predicate.PredicateDefinition;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.util.List;

/**
 * @Description: java类作用描述
 * @Author: gaozb
 * @Date: 2021/9/10$ 17:36$
 * @Version: 1.0
 */
@RestController
@Api(tags = "动态路由管理")
public class SysRouteController {


    @Autowired
    private SysRouteConfService sysRouteConfService;


    @PostMapping("/route/add")
    @PreAuthorize("hasAuthority('sys_route_create')")
    public R add(@Validated @RequestBody RouteDefinition route) {
        if(route.getId() == null) {
            return R.fail("路由id不能为空");
        }
        Boolean add = false;
        try {
            add = sysRouteConfService.add(route);
        } catch (CustomCmdiAdminException e) {
            add = false;
        }
        return add ? R.ok(route, "添加路由成功") : R.fail("添加路由失败");
    }

    @GetMapping("/route/delete")
    @PreAuthorize("hasAuthority('sys_route_delete')")
    public R change(@RequestParam("id") String id) {
        Boolean del = false;
        try {
            del = sysRouteConfService.del(id);
        } catch (CustomCmdiAdminException e) {
            del = false;
        }
        return del ? R.ok(id, "删除路由成功, id="+ id) : R.fail("删除路由失败, id="+ id);
    }

    @GetMapping("/route/sync")
    @PreAuthorize("hasAuthority('sys_route_sync')")
    public R<Boolean> flush() {
        return R.ok(sysRouteConfService.flush());
    }

}